package com.yidu.best.systemManagement.Authority.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.best.systemManagement.Authority.domain.AuthorityResource;
import com.yidu.best.systemManagement.Authority.domain.AuthorityRole;
import com.yidu.best.systemManagement.Authority.domain.AuthorityRoleResource;
import com.yidu.best.systemManagement.Authority.domain.AuthorityUser;
import com.yidu.best.systemManagement.Authority.domain.AuthorityUserRole;
import com.yidu.best.systemManagement.Authority.service.AuthorityService;

@Controller
@RequestMapping("/sys/")
public class AuthorityController {
	@Autowired
	private AuthorityService authorityService;

	public AuthorityService getAuthorityService() {
		return authorityService;
	}

	public void setAuthorityService(AuthorityService authorityService) {
		this.authorityService = authorityService;
	}

	@RequestMapping("Authority")
	public String userAndRoleMap() {
		return "/sys/resource/AccessTheResource";
	}

	@ResponseBody
	@RequestMapping(value = "selects", method = RequestMethod.GET)
	public Map<String, Object> queryUser() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<AuthorityUser> queryUser = authorityService.queryUser();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", queryUser);
		return map;
	}

	/**
	 * 条件查询
	 * 
	 * @param page
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "selectss", method = RequestMethod.GET)
	public Map<String, Object> queryUser(String userName, String roleName, Integer useable) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<AuthorityUser> queryUser = authorityService.queryUsers(userName, roleName, useable);
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", queryUser);
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "userRole", method = RequestMethod.GET)
	public List<AuthorityUser> queryUserWithRole(String userId) {
		List<AuthorityUser> withRole = authorityService.queryUserWithRole(userId);
		return withRole;
	}

	@ResponseBody
	@RequestMapping(value = "roles", method = RequestMethod.GET)
	public List<AuthorityRole> selectRoleName() {
		return authorityService.selectRoleName();
	}

	@ResponseBody
	@RequestMapping(value = "setUserRole", method = RequestMethod.POST)
	public int setUserRole(AuthorityUserRole authorityUserRole, String role) {
		return authorityService.setUserRole(authorityUserRole);

	}

	@ResponseBody
	@RequestMapping(value = "updata", method = RequestMethod.POST)
	public int updata(AuthorityUserRole authorityUserRole, String role) {
		return authorityService.updateRoleName(authorityUserRole);
	}

	@ResponseBody
	@RequestMapping(value = "getroleId/{rName}")
	public String getroleId(@PathVariable("rName") String name) {
		List<AuthorityRole> selectRoleName = authorityService.selectRoleName();
		String roleid = null;
		for (AuthorityRole authorityRole : selectRoleName) {
			if (authorityRole.getRoleName().equals(name)) {
				roleid = authorityRole.getRoleId();
			}
		}
		return roleid;
	}

	@ResponseBody
	@RequestMapping(value = "RoleResource", method = RequestMethod.GET)
	public List<AuthorityResource> queryRoleResource(AuthorityResource authorityResource) {
		List<AuthorityResource> authorityResourceList = authorityService.queryRoleResource(authorityResource);
		for (AuthorityResource authorityResource2 : authorityResourceList) {
			if (authorityResource2.getParentId() == null) {
				authorityResource2.setParentId("0");
			}
		}
		return authorityResourceList;
	}

	@ResponseBody
	@RequestMapping(value = "RoleWithResources", method = RequestMethod.GET)
	public List<AuthorityRole> queryRoleWithResource() {
		return authorityService.queryRoleWithResource();
	}

	@ResponseBody
	@RequestMapping(value = "updateuseable", method = RequestMethod.POST)
	public int updateUseable(AuthorityUser authorityUser) {
		return authorityService.updateUseable(authorityUser);
	}

	@ResponseBody
	@RequestMapping(value = "addRoleByResource", method = RequestMethod.POST)
	public int addRoleByResource(AuthorityRoleResource authorityRoleResource) {
		return authorityService.addRoleByResource(authorityRoleResource);
	}

	@ResponseBody
	@RequestMapping(value = "userRoleResource", method = RequestMethod.GET)
	public List<AuthorityResource> selectUserInRoleByResource(String userName) {
		return authorityService.selectUserInRoleByResource(userName);
	}
}
